import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: {
      username: window.localStorage.getItem('user' || '') === null ? '' : window.localStorage.getItem('user' || '')
    },
    admin: {
      adminname: window.localStorage.getItem('admin' || '') === null ? '' : window.localStorage.getItem('admin' || '')
    },
    color: window.localStorage.getItem('color' || '') === null ? '1' : window.localStorage.getItem('color' || '')
  },
  mutations: {
    userlogin (state, user) {
      state.user.username = user.username
      window.localStorage.setItem('user', user.username )
      window.localStorage.setItem('time', JSON.stringify(new Date().getTime()));
      window.localStorage.setItem('admin', '')
    },
    adminlogin (state, admin) {
      state.admin.adminname = admin.username
      window.localStorage.setItem('admin', admin.username)
      window.localStorage.setItem('time', JSON.stringify(new Date().getTime()));
      window.localStorage.setItem('user', '')
    },
    changeColor(state, color) {
      state.color = color;
      window.localStorage.setItem('color', color)
    }
  }
})
